package com.jie.other.compose.study03

import androidx.compose.runtime.toMutableStateList
import androidx.lifecycle.ViewModel
import com.jie.other.compose.data.TaskItemDTO

class Study03ViewModel : ViewModel() {

    private val _mTaskList = getTaskList().toMutableStateList()

    val mTaskList: List<TaskItemDTO>
        get() = _mTaskList

    fun removeTask(taskItemDTO: TaskItemDTO) {
        _mTaskList.remove(taskItemDTO)
    }

    fun addTask() {
        val id = if (_mTaskList.isNotEmpty()) {
            _mTaskList[_mTaskList.lastIndex].id + 1
        } else {
            1
        }
        _mTaskList.add(
            TaskItemDTO(
                id = id,
                taskName = "Task # $id"
            )
        )
    }

    fun clearTask() {
        _mTaskList.clear()
    }

    private fun getTaskList() : List<TaskItemDTO> {
        return List(30) {
            TaskItemDTO(it, "Task # $it")
        }
    }
}